package com.example.star.view.center;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.example.star.R;
import com.example.star.view.activity.LoginActivity;
import com.example.star.view.setting.AboutActivity;
import com.example.star.view.setting.AddressActivity;
import com.example.star.view.setting.PersonalActivity;
import com.example.star.view.setting.FeedbackActivity;
import com.example.star.view.setting.UpdatePasswordActivity;
import com.example.star.view.setting.UserAgreement;

public class SettingsActivity extends AppCompatActivity {

    private ImageButton returnsButton;
    private RelativeLayout personalInfoButton;
    private RelativeLayout addressManagerButton;
    private RelativeLayout loginPasswordButton;
    private RelativeLayout feedbackButton;
    private RelativeLayout aboutUsButton;

    private RelativeLayout URSButton;
    private Button logoutButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 初始化按钮
        returnsButton = findViewById(R.id.returns);
        personalInfoButton = findViewById(R.id.personal_info);
        addressManagerButton = findViewById(R.id.address_manager);
        loginPasswordButton = findViewById(R.id.login_password);
        feedbackButton = findViewById(R.id.feedback);
        aboutUsButton = findViewById(R.id.about_us);
        URSButton = findViewById(R.id.URS);

        // 在onCreate方法中初始化按钮
        logoutButton = findViewById(R.id.logout_button);

        // 设置返回按钮的点击事件
        returnsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 结束当前活动并返回
            }
        });

        // 为其他按钮设置点击事件
        personalInfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到个人信息页面的逻辑
                Intent intent = new Intent(SettingsActivity.this, PersonalActivity.class);
                startActivity(intent);
            }
        });

        addressManagerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到地址管理页面的逻辑
                Intent intent = new Intent(SettingsActivity.this, AddressActivity.class);
                startActivity(intent);
            }
        });

        loginPasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到登录密码设置页面的逻辑
                Intent intent = new Intent(SettingsActivity.this, UpdatePasswordActivity.class);
                startActivity(intent);
            }
        });
        feedbackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到意见反馈页面的逻辑
                Intent intent = new Intent(SettingsActivity.this, FeedbackActivity.class);
                startActivity(intent);
            }
        });

        aboutUsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到关于我们页面的逻辑
                Intent intent = new Intent(SettingsActivity.this, AboutActivity.class);
                startActivity(intent);
            }
        });

        URSButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示用户协议的逻辑
                Intent intent = new Intent(SettingsActivity.this, UserAgreement.class);
                startActivity(intent);
            }
        });


// 设置退出登录按钮的点击事件
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutConfirmationDialog();
            }
        });
        // ... 其他按钮的点击事件以此类推
    }

    private void showLogoutConfirmationDialog() {
        new AlertDialog.Builder(this)
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 用户点击确定，执行退出登录操作
                        logout();
                    }
                })
                .setNegativeButton("取消", null) // 点击取消，对话框消失，不执行任何操作
                .show();
    }

    private void logout() {
        // 关闭所有Activity并回到登录界面
        // 获取当前Activity的Intent
        Intent loginIntent = new Intent(SettingsActivity.this, LoginActivity.class);
        // 确保使用FLAG_ACTIVITY_CLEAR_TOP，这样当新的Intent启动时，如果在任务栈中有该Activity的实例，则不会重新创建，而是重用这个Activity实例
        loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // 使用finishAffinity来关闭所有栈顶的Activity
        finishAffinity();
        // 使用startActivity启动登录Activity
        startActivity(loginIntent);
        // 如果需要，可以在这里添加一个成功退出登录的Toast提示或其他反馈
    }
}